package com.zb.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zb.entity.*;
import com.zb.order.service.ConWorkModuleService;
import com.zb.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author WEN
 * @Date 2023/10/30 0030
 **/
@Service
public class ConWorkModuleServiceImpl implements ConWorkModuleService {
    @Resource
    ZjWorkModuleService workModuleService;
    @Resource
    private ZjWorkOrderService workOrderService;
    @Resource
    private ZjWorkStatementTemplateModuleService workStatementTemplateModuleService;
    @Resource
    private ZjWorkStatementTemplateService workStatementTemplateService;
    @Resource
    private ZjWorkBackVisitTemplateModuleService workBackVisitTemplateModuleService;
    @Resource
    private ZjWorkBackVisitTemplateService workBackVisitTemplateService;
    @Resource
    private ZjNetworkModuleSettlementService networkModuleSettlementService;


    // 根据工单模块ID删除
    @Override
    public void deleteBatchById(List<Long> ids) {
        // 关联工单
        List<Long> moduleIds =  workOrderService.getRelationModuleIdByModuleIds(ids);
        if (CollectionUtils.isNotEmpty(moduleIds)) {
            QueryWrapper<ZjWorkModule> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", moduleIds);
            String moduleName = workModuleService.list(wrapper).stream().map(ZjWorkModule::getName).collect(Collectors.joining("、"));
            Assert.isTrue(false, "模块名称[" + moduleName + "]已被工单关联使用中，不可删除");
        }
        // 结单模板
        List<ZjWorkStatementTemplateModule> statementTemplateModules = workStatementTemplateModuleService.getByModuleIds(ids);
        if (CollectionUtils.isNotEmpty(statementTemplateModules)) {
            moduleIds = statementTemplateModules.stream().map(ZjWorkStatementTemplateModule::getModuleId).distinct().toList();
            List<Long> templateIds = statementTemplateModules.stream().map(ZjWorkStatementTemplateModule::getWorkStatementTemplateId).distinct().toList();
            String templateNames = workStatementTemplateService.getByIds(templateIds).stream().map(ZjWorkStatementTemplate::getName).collect(Collectors.joining("、"));
            QueryWrapper<ZjWorkModule> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", moduleIds);
            String moduleName = workModuleService.list(wrapper).stream().map(ZjWorkModule::getName).collect(Collectors.joining("、"));
            Assert.isTrue(false, "模块名称[" + moduleName + "]已与结单模板[" + templateNames + "]关联使用中，不可删除");
        }
        // 回访模板
        List<ZjWorkBackVisitTemplateModule> visitTemplateModules = workBackVisitTemplateModuleService.getByModuleIds(ids);
        if (CollectionUtils.isNotEmpty(visitTemplateModules)) {
            moduleIds = visitTemplateModules.stream().map(ZjWorkBackVisitTemplateModule::getModuleId).distinct().toList();
            List<Long> templateIds = visitTemplateModules.stream().map(ZjWorkBackVisitTemplateModule::getWorkBackVisitTemplateId).distinct().toList();
            String templateNames = workBackVisitTemplateService.getByIds(templateIds).stream().map(ZjWorkBackVisitTemplate::getName).collect(Collectors.joining("、"));
            QueryWrapper<ZjWorkModule> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", moduleIds);
            String moduleName = workModuleService.list(wrapper).stream().map(ZjWorkModule::getName).collect(Collectors.joining("、"));
            Assert.isTrue(false, "模块名称[" + moduleName + "]已与回访模板[" + templateNames + "]关联使用中，不可删除");
        }
        // 网点服务商结算价
        moduleIds = networkModuleSettlementService.getRelationByModuleIds(ids);
        if (CollectionUtils.isNotEmpty(moduleIds)) {
            QueryWrapper<ZjWorkModule> wrapper = new QueryWrapper<>();
            wrapper.select("name");
            wrapper.in("id", moduleIds);
            String moduleName = workModuleService.list(wrapper).stream().map(ZjWorkModule::getName).collect(Collectors.joining("、"));
            Assert.isTrue(false, "模块名称[" + moduleName + "]已被服务商关联使用中，不可删除");
        }
        workModuleService.removeByIds(ids);
    }

}
